using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using common = Microsoft.ConnectionEngine.Common;
using dao = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using business = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Business;

using Microsoft.ConnectionEngine.Common.Security;


namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Facade
{
    public class ServiceProviderManager
    {

        //ServiceProviderListRequest   1000
        public static void GetAllowedProviders(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {

            dao.ServiceProviderList result = new dao.ServiceProviderList();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode searchNode = xmlMessage.SelectSingleNode("//pnp:ServiceProviderID", nsmgr);

            result = business.ServiceProviderManager.GetAllowedServiceProviders(searchNode.InnerText);

            resultMsgType = 1001;
            resultXmlMessage = new XmlDocument();
            resultXmlMessage.LoadXml(common.Utilities.SerializeObject(result, result.GetType()));
        }

        //InteractiveSessionURIRequest   9026
        public static void GetInteractiveSessionURIList(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {

            string result = string.Empty;

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode searchNode = xmlMessage.SelectSingleNode("//pnp:ServiceProviderID", nsmgr);

            result = business.ServiceProviderManager.GetInteractiveSessionList(searchNode.InnerText);

            resultMsgType = 9027;
            resultXmlMessage = new XmlDocument();
            resultXmlMessage.LoadXml(result);
        }

        //HSDReferenceDataRequest 1002
        public static void GetReferenceData(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {

            dao.ReferenceData result = new dao.ReferenceData();


            result = business.RefDataManager.GetRefData();

            resultMsgType = 1003;
            resultXmlMessage = new XmlDocument();
            resultXmlMessage.LoadXml(common.Utilities.SerializeObject(result, result.GetType()));
        }

        //ServiceProviderSearchRequest 1014
			public static void ServiceProviderSearch(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				dao.ServiceProviderSearchRequest request = new dao.ServiceProviderSearchRequest();
                dao.ServiceProviders serviceProviders = new dao.ServiceProviders(); 

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				//HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
                Utilities.CreateUser(userCredentials, nsmgr);  

				XmlNode searchNode = xmlMessage.SelectSingleNode("//pnp:ServiceProviderSearchRequest", nsmgr);
                request = (dao.ServiceProviderSearchRequest)common.Utilities.DeserializeObject(searchNode.OuterXml, typeof(dao.ServiceProviderSearchRequest));

                serviceProviders = business.ServiceProviderManager.ServiceProviderSearch(request);

                resultMsgType = (int)dao.MessageTypes.ServiceProviderSearchResult;
				resultXmlMessage = new XmlDocument();
				resultXmlMessage.LoadXml(common.Utilities.SerializeObject(serviceProviders, typeof(dao.ServiceProviders)));
			}

            //ServiceProviderSelectRequest 1070
			public static void ServiceProviderSelectItem(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				dao.ServiceProvider serviceProvider = new dao.ServiceProvider();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				//HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
                Utilities.CreateUser(userCredentials, nsmgr); 

				String serviceProviderID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderID", nsmgr).InnerText;
                serviceProvider = business.ServiceProviderManager.GetServiceProviderByID(serviceProviderID);		
											
                resultMsgType = (int)dao.MessageTypes.ServiceProviderSelectResult;
				resultXmlMessage = new XmlDocument();
				resultXmlMessage.LoadXml(common.Utilities.SerializeObject(serviceProvider, typeof(dao.ServiceProvider)));
			}

            //ServiceProviderUnlock 1007
			public static void ServiceProviderUnlock(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{				
				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
                
				String serviceProviderID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderID", nsmgr).InnerText;
                //String status = business.ServiceProviderManager.ServiceProviderUnlock(serviceProviderID, currentUser.HSDIdentity.UserId);
                business.ServiceProviderManager.ServiceProviderUnlock(serviceProviderID, currentUser.HSDIdentity.UserId);
			
				resultMsgType = 0;
				resultXmlMessage = new XmlDocument();				
			}

            //ServiceProviderUnlockAll 1040
			public static void ServiceProviderUnlockAll(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{				
				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
                
                //String status = business.ServiceProviderManager.ServiceProviderUnlockAll(currentUser.HSDIdentity.UserId);
                business.ServiceProviderManager.ServiceProviderUnlockAll(currentUser.HSDIdentity.UserId);

				resultMsgType = 0;
				resultXmlMessage = new XmlDocument();				
			}

            //ServiceProviderLock 1008
			public static void ServiceProviderLock(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				dao.ServiceProvider serviceProvider = new dao.ServiceProvider();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
                

				String serviceProviderID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderID", nsmgr).InnerText;
                //String status = business.ServiceProviderManager.ServiceProviderLock(serviceProviderID, currentUser.HSDIdentity.UserId);
                business.ServiceProviderManager.ServiceProviderLock(serviceProviderID, currentUser.HSDIdentity.UserId);

                serviceProvider = business.ServiceProviderManager.GetServiceProviderByID(serviceProviderID);
											
				resultMsgType = (int)dao.MessageTypes.ServiceProviderLockResult;
				resultXmlMessage = new XmlDocument();
				resultXmlMessage.LoadXml(common.Utilities.SerializeObject(serviceProvider, typeof(dao.ServiceProvider)));
			}

            //ServiceProviderInsert 1010
			public static void ServiceProviderInsert(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				dao.ServiceProvider serviceProvider = new dao.ServiceProvider();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   

				XmlNode serviceProviderNode = xmlMessage.SelectSingleNode("//pnp:ServiceProvider", nsmgr);
                serviceProvider = (dao.ServiceProvider)common.Utilities.DeserializeObject(serviceProviderNode.OuterXml, typeof(dao.ServiceProvider));
                
                String status = business.ServiceProviderManager.InsertServiceProvider(serviceProvider, currentUser.HSDIdentity.UserId);				
											
				resultMsgType = (int)dao.MessageTypes.ServiceProviderInsertResult;
				resultXmlMessage = new XmlDocument();
				resultXmlMessage.LoadXml(common.Utilities.SerializeObject(serviceProvider, typeof(dao.ServiceProvider)));
			}

            //ServiceProviderUpdate 1009
			public static void ServiceProviderUpdate(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				dao.ServiceProvider serviceProvider = new dao.ServiceProvider();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   

				XmlNode serviceProviderNode = xmlMessage.SelectSingleNode("//pnp:ServiceProvider", nsmgr);
				serviceProvider = (dao.ServiceProvider)common.Utilities.DeserializeObject(serviceProviderNode.OuterXml, typeof(dao.ServiceProvider));
                
                String status = business.ServiceProviderManager.UpdateServiceProvider(serviceProvider, currentUser.HSDIdentity.UserId);				
											
				resultMsgType = (int)dao.MessageTypes.ServiceProviderUpdateResult;
				resultXmlMessage = new XmlDocument();
				resultXmlMessage.LoadXml(common.Utilities.SerializeObject(serviceProvider, typeof(dao.ServiceProvider)));
			}

            //ServiceProviderDelete 1006
			public static void ServiceProviderDelete(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{   	
				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   

				String serviceProviderID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderID", nsmgr).InnerText;
                //String status = business.ServiceProviderManager.DeleteServiceProvider(serviceProviderID, currentUser.HSDIdentity.UserId);
                business.ServiceProviderManager.DeleteServiceProvider(serviceProviderID, currentUser.HSDIdentity.UserId);  								
			
				resultMsgType = 0;
				resultXmlMessage = new XmlDocument();				
			}									

    }
}
